//
//  MapViewController.m
//  Bicycle
//
//  Created by dllo on 16/4/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <CoreLocation/CoreLocation.h>





@interface MapViewController ()<MAMapViewDelegate,CLLocationManagerDelegate>
//大地图
@property(nonatomic,retain)MAMapView *mapView;
//建立个数组一会用来装行进的点
@property(nonatomic,retain)NSMutableArray *pointsArr;
//在地图上绘制线的数据模型,是地图覆盖物的一种
@property(nonatomic,retain)MAPolyline *routeLine;
//在地图上绘制线的样式,是MAPolylineView的一种
@property(nonatomic,retain) MAPolylineView* routeLineView;
//定位0
@property(nonatomic,retain)CLLocation *currentLocation;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [MAMapServices sharedServices].apiKey = @"517da233d8b139d0e1f532e716d0d9ce";
    [AMapSearchServices sharedServices].apiKey = @"517da233d8b139d0e1f532e716d0d9ce";
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), (CGRectGetHeight(self.view.bounds))/2)];
    _mapView.delegate = self;
    self.mapView.delegate = self;
     self.mapView.showsUserLocation = YES;
    self.mapView.userInteractionEnabled = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;

    [self.view addSubview:_mapView];
    
    _mapView.showsUserLocation=YES;
    //地图跟着位置移动
    [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];

    [self configureRoutes];
    
    
    
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    
    self.mapView = nil;
    self.routeLine = nil;
    self.routeLineView = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


-(void)configureRoutes{

    MAMapPoint northEastPoint = MAMapPointMake(0.f, 0.f);
  MAMapPoint southWestPoint = MAMapPointMake(0.f, 0.f);
    MAMapPoint *pointArr=malloc(sizeof(CLLocationCoordinate2D)*_pointsArr.count);
    for (int i=0; i<_pointsArr.count; i++) {
        CLLocation *location=[_pointsArr objectAtIndex:i];
        CLLocationDegrees latitude=location.coordinate.latitude;
        CLLocationDegrees longitude=location.coordinate.longitude;
        
        CLLocationCoordinate2D cooedinate=CLLocationCoordinate2DMake(latitude, longitude);
        MAMapPoint point=MAMapPointForCoordinate(cooedinate);
        
        if (i==0) {
                        northEastPoint=point;
            southWestPoint=point;
        }else{

            if (point.x > northEastPoint.x)
                northEastPoint.x = point.x;
            if(point.y > northEastPoint.y)
                northEastPoint.y = point.y;
            if (point.x < southWestPoint.x)
                southWestPoint.x = point.x;
            if (point.y < southWestPoint.y)
                southWestPoint.y = point.y;
                       }
        pointArr[i] = point;
        NSLog(@"%f",point.y);
        }
    if (self.routeLine) {
        [self.mapView removeOverlay:self.routeLine];
    }

    
         self.routeLine = [MAPolyline polylineWithPoints:pointArr count:_pointsArr.count];
        
    if (nil != self.routeLine) {
        [self.mapView addOverlay:self.routeLine];
    }
         free(pointArr);
        
  }

-(void)mapView:(MAMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews{



}

//实现MAMapView回调方法

- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
{
    //    NSLog(@"%@ ----- %@", self, NSStringFromSelector(_cmd));
    
    MAOverlayView* overlayView = nil;
    
    if(overlay == self.routeLine)
    {
        //if we have not yet created an overlay view for this overlay, create it now.
        if (self.routeLineView) {
            [self.routeLineView removeFromSuperview];
        }
        
        self.routeLineView = [[MAPolylineView alloc] initWithPolyline:self.routeLine];
        self.routeLineView.fillColor = [UIColor redColor];
        self.routeLineView.strokeColor = [UIColor yellowColor];
        self.routeLineView.lineWidth = 10;
        
        overlayView = self.routeLineView;
    }
    
    return overlayView;
}


- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    NSLog(@"%@ ----- %@", self, NSStringFromSelector(_cmd));
    NSLog(@"annotation views: %@", views);
}



//定位实现的方法
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
{
    
    UIGraphicsBeginImageContext(self.view.frame.size); //currentView 当前的view
    [self.routeLineView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    //    viewImage就是获取的截图，如果要将图片存入相册，只需在后面调用
    UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);
    
    
    
    CLLocation *location=[[CLLocation alloc]initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];

    
    if (userLocation.coordinate.latitude==0.0f||userLocation.coordinate.longitude==0.0f)
        return;
    
    if (_pointsArr.count>0) {
        CLLocationDirection distance=[location distanceFromLocation:_currentLocation];
        
        if (distance<5)
            return;
        }
        
        if (nil==_pointsArr) {
            _pointsArr=[[NSMutableArray alloc]init];
        }
       
    
    
    

    [_pointsArr addObject:location];
    _currentLocation=location;
    [self configureRoutes];
    
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    
    
    
    
    [self.mapView setCenterCoordinate:coordinate animated:YES];




}


































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
